const mysql = require("mysql");

const { MYSQL_CONFIG } = require("../config/index");

const pool = mysql.createPool(MYSQL_CONFIG);

const query = function (sql, data) {
  return new Promise(function (resolve, reject) {
    pool.getConnection(function (err, conncetion) {
      if (!err) {
        conncetion.query(sql, data, function (error, result) {
          if (!error) {
            resolve(result);
          } else {
            console.log("数据库查询错误：", err);
            reject(error);
          }
          conncetion.release();
        });
      } else {
        console.log("数据库连接错误：", err);
        reject(err);
      }
    });
  });
};

module.exports = query;
